package com.yunzheng.controller;

import com.yunzheng.common.BaseResult;
import com.yunzheng.dto.NoticeChangeDTO;
import com.yunzheng.dto.NoticePostDTO;
import com.yunzheng.entity.ProjectNotice;
import com.yunzheng.service.ProjectNoticeService;
import com.yunzheng.util.SnowFlakeUtil;
import com.yunzheng.util.TokenParser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author 李美琪
 * @date 2020/7/27 9:36
 */

@Api(tags = "公告")
@RestController
@RequestMapping("/notice")
public class ProjectNoticeController {

    @Resource
    private ProjectNoticeService projectNoticeService;

    /**
     * 发布公告
     *
     * /post -- authc
     */
    @ApiOperation(value = "发布公告")
    @PostMapping("/post")
    public BaseResult noticePost(HttpServletRequest request, @Valid @RequestBody NoticePostDTO in){
//        projectNoticeService.noticePost(in.getNoticeTitle(),in.getNoticeContent(),in.getNoticeProject(),in.getIsTop(), TokenParser.token2UserId(request));
        String userId = TokenParser.token2UserId(request);
        ProjectNotice notice = new ProjectNotice();
        BeanUtils.copyProperties(in, notice);
        notice.setNoticer(userId).setNoticeId(SnowFlakeUtil.nextId());
        projectNoticeService.save(notice);
        return BaseResult.ok();
    }

    /**
     * 展示公告
     *
     * /show -- authc
     */
    @ApiOperation(value = "展示公告")
    @GetMapping("/show")
    public BaseResult noticeShow(@RequestParam String projectId){
        return BaseResult.ok(projectNoticeService.noticeShow(projectId));
    }

    /**
     * 修改公告
     *
     * /change -- authc
     */
    @ApiOperation(value = "修改公告")
    @PutMapping("/change")
    public BaseResult noticeChange(@Valid @RequestBody NoticeChangeDTO in){
//        projectNoticeService.noticeChange(in.getNoticeId(),in.getNoticeTitle(),in.getNoticeContent(),in.getIsTop());
        ProjectNotice notice = new ProjectNotice();
        BeanUtils.copyProperties(in, notice);
        projectNoticeService.saveOrUpdate(notice);
        return BaseResult.ok();
    }

    /**
     * 删除公告
     *
     * /remove -- authc
     */
    @ApiOperation(value = "删除公告")
    @DeleteMapping("/remove")
    public BaseResult noticeRemove(@RequestParam String noticeId){
//        projectNoticeService.noticeRemove(noticeId);
        projectNoticeService.removeById(noticeId);
        return BaseResult.ok();
    }
}
